package bll

import (
	"encoding/json"
	"fmt"
	"os"
	"time"

	"yingsheng.com/go-api-server"
	"yingsheng.com/go-api-server/errors"
	"yingsheng.com/go-api-server/example/dal"
	"yingsheng.com/go-api-server/example/model"
	"yingsheng.com/go-api-server/log"
	"yingsheng.com/go-api-server/mq"
	"yingsheng.com/go-api-server/orm"
	"yingsheng.com/go-api-server/utils"
)

var (
	Test TestBL
)

type TestBL struct {
	producer mq.ProducerInterface
}

func init() {
	apiserver.Wait()

	var err error

	// 注册RPC
	apiserver.AddRpcService(&Test)

	// 创建消息发布者对象
	Test.producer, err = mq.NewProducer("nsq", "example")
	if err != nil {
		log.Logger.Critical("Producer New Error: ", err)
		os.Exit(1)
	} else {
		log.Logger.Info("Producer running...")
	}
}

func (t *TestBL) PublishMessage() error {
	data := make(map[string]interface{})
	data["username"] = "zaaksam"
	data["age"] = 100
	data["timestamp"] = time.Now().Unix()

	j, err := json.Marshal(data)
	if err != nil {
		return err
	}

	return t.producer.Publish(j)
}

func (*TestBL) Count() time.Duration {
	//可以在这里放置其他业务逻辑代码
	fmt.Println("每隔1小时打印一次")

	return 1 * time.Hour
}

func (*TestBL) Add(userId int64, userName string) (*model.TestMD, error) {
	da, err := dal.NewTestDA()
	if err != nil {
		return nil, err
	}

	//md是插入数据实体
	md := &model.TestMD{
		UserId:     userId,
		UserName:   userName,
		CreateTime: utils.GetTimeByString(),
	}
	affected, err := da.Insert(md)
	if err != nil {
		return nil, err
	} else if affected <= 0 {
		return nil, errors.New("数据插入失败")
	}

	return md, nil
}

func (*TestBL) Update(userId int64, userName string) error {
	da, err := dal.NewTestDA()
	if err != nil {
		return err
	}

	da.Set("UserName,CreateTime", userName, utils.GetTimeByString())

	//Or Set like this
	//	da.Set("UserName", userName).Set("CreateTime", utils.GetTimeByString())

	//Or Set like this
	//	da.Set("UserName", userName)
	//	da.Set("CreateTime", utils.GetTimeByString())

	affected, err := da.UpdateByUserId(userId)

	//Quick Update
	//	affected, err := da.Set("", "").Where("Username=?", userName).Update()

	if err != nil {
		return err
	} else if affected <= 0 {
		return errors.New("数据更新失败")
	}

	return nil
}

func (*TestBL) Delete(userName string) error {
	da, err := dal.NewTestDA()
	if err != nil {
		return err
	}

	//md是删除条件
	md := &model.TestMD{
		UserName: userName,
	}
	affected, err := da.Delete(md)

	if err != nil {
		return err
	} else if affected <= 0 {
		return errors.New("数据删除失败")
	}

	return nil
}

func (*TestBL) Get(id int64) (*model.TestMD, error) {
	da, err := dal.NewTestDA()
	if err != nil {
		return nil, err
	}

	md := &model.TestMD{
		Id: id,
	}
	has, err := da.Get(md)
	if err != nil {
		return nil, err
	} else if !has {
		return nil, errors.New("没有数据")
	}

	return md, nil
}

func (*TestBL) ListByUserName(pageIndex, pageSize int, userName string) (*orm.ModelList, error) {
	da, err := dal.NewTestDA()
	if err != nil {
		return nil, err
	}

	return da.ListLikeUserName(pageIndex, pageSize, userName)
}

func (*TestBL) ListByCreateTime(pageIndex, pageSize int, startTime, endTime string) (*orm.ModelList, error) {
	da, err := dal.NewTestDA()
	if err != nil {
		return nil, err
	}

	return da.ListBetweenCreateTime(pageIndex, pageSize, startTime, endTime)
}
